const { findUser, createUser, updateUser } = require("../db/user");
const { isRequiredParams } = require("../utils/tool");
const { createToken, checkToken } = require("../utils/jwt");
const { SuccessModel, ErrorModel } = require("../model/resModel");

const UserCtl = {
  /**
   * @api {post} /user/login 登录接口
   * @apiName 登录接口
   * @apiGroup User
   *
   * @apiParam {string} us 登录账号【必填】
   * @apiParam {string} ps 密码
   *
   * @apiSuccess {JSON} 自己看返回.
   */
  login: async (ctx) => {
    const { phone, ps } = ctx.request.body;
    // 先查询有没有用户
    let user = await findUser({ phone }, true);
    if (user.length <= 0) {
      ctx.body = new ErrorModel("账号不存在");
      return;
    }
    if (user[0]?.ps !== ps) {
      ctx.body = new ErrorModel("密码错误");
      return;
    }
    user = user[0];
    delete user.ps;
    const token = await createToken({ id: user.id });
    ctx.body = new SuccessModel({
      user,
      token,
    });
  },
  /**
   * @api {post} /user/create 新建用户
   * @apiName 新建用户
   * @apiGroup User
   *
   * @apiParam {string} name 用户名【必填】
   * @apiParam {string} us 登录账号【必填】
   * @apiParam {string} ps 密码
   * @apiParam {number} station 职位
   *
   * @apiSuccess {JSON} 自己看返回.
   */
  create: async (ctx) => {
    const body = ctx.request.body;
    const { err, msg } = isRequiredParams(["name", "us"], body);
    if (err) {
      ctx.body = new ErrorModel(msg);
      return;
    }
    const user = await findUser({ us: body.us });
    if (user.length > 0) {
      ctx.body = new ErrorModel("当前注册信息已存在用户！");
      return;
    }
    let res = await createUser(body);
    if (res.err) {
      ctx.body = new ErrorModel("创建失败！");
      return;
    }
    ctx.body = new SuccessModel(res, "创建成功");
  },
  /**
   * @api {get} /user/find 查询用户
   * @apiName 查询用户
   * @apiGroup User
   *
   * @apiParam {Number} limit 每页数量
   * @apiParam {Number} page 第几页
   * @apiParam {String} id 用户id
   * @apiParam {String} name 姓名
   * @apiParam {String} phone 电话
   * @apiParam {String} us 登录名
   * @apiParam {Number} station 职位
   *
   * @apiSuccess {JSON} 自己看返回.
   */
  find: async (ctx) => {
    const params = ctx.query;
    const res = await findUser(params);
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: "查询失败！",
        data: [],
      };
      return;
    }
    ctx.body = {
      err: 0,
      data: res,
    };
  },
  /**
   * @api {post} /user/create 修改用户信息
   * @apiName 修改用户信息
   * @apiGroup User
   *
   * @apiParam {string} id 用户ID【必填】
   * @apiParam {string} name 姓名
   * @apiParam {string} ps 密码
   * @apiParam {String} phone 电话
   * @apiParam {number} station 岗位
   * @apiParam {number} chargeTimes 服务次数
   * @apiParam {Array} permission 权限
   *
   * @apiSuccess {JSON} 自己看返回.
   */
  update: async (ctx) => {
    const body = ctx.request.body;
    const { err, msg } = isRequiredParams(["id"], body);
    if (err) {
      ctx.body = {
        err: -1,
        msg,
      };
      return;
    }
    const res = await updateUser(body);
    if (res.err) {
      ctx.body = {
        err: -999,
        msg: "修改用户信息失败！",
        data: {},
      };
      return;
    }
    ctx.body = {
      err: 0,
      msg: "修改用户信息成功",
      data: res,
    };
  },
  isLogin: async (ctx) => {
    const { authorization: token } = ctx.headers;
    try {
      const checkRes = await checkToken(token);
      ctx.body = {
        err: 0,
        data: {
          ...checkRes,
          isLogin: true,
        },
      };
    } catch ({ code, msg }) {
      ctx.body = {
        err: -99,
        msg: msg,
      };
      return;
    }
  },
};

module.exports = UserCtl;
